<?php
class cluster_form_parameter
{
	public $id;
	public $sort;
	public $name;

	public function get_form_name()
	{
		$pre =($sort!= 'none')? 'uniform_chain_' : 'chain_';
		return $pre.$name.".".$id;
	}
}


class cluster_form
{
	public $link_up;
	public $characteristics = array();
	public $chains = array();

	public function create($form)
	{
		$this->link_up = strtolower($form['chain_value_type_select']);
		if (!empty($_SESSION['chain_count'])) {
			$counter = 1;
			while ($counter<=$_SESSION['chain_count']) {
				$chains_array[] = $form['chain_count'.$counter];
				$counter++;
			}
		}
		$this->chains = $chains_array;
		
		foreach ($form as $name => $value) {
			if ($value == 'checked')
			{
				$names = explode('.', $name);
				$type = $names[0];
				$temp = new cluster_form_parameter();
				$temp->name = $names[1];
				$temp->id = $names[2];
				if ($type == 'uniform_chain')
				{
					$temp->sort = $form[$name.".select"];
				}else
				{
					$temp->sort = 'none';
				}
				$this->characteristics[$temp->id] = $temp;
			}
		}
	}
	
	public function save_new_clusterisation(){
		$dba = db_adapter::get_instance();
		$clusterisation = new clusterisation();
		$clusterisation->linkup->set_value($this->link_up);
		$dba->save($clusterisation);
		
		foreach ($this->chains as $chain_id){
			$clusterisation_chain = new clusterisation_chain();
			$clusterisation_chain->clusterisation_id->set_value($clusterisation->clusterisation_id->get_value());
			$clusterisation_chain->chain_id->set_value($chain_id);
			$dba->save($clusterisation_chain);
		}
		
		foreach ($this->characteristics as $characteristic){
			$clusterisation_param = new clusterisation_parametrs();
			$clusterisation_param->clusterisation_id->set_value($clusterisation->clusterisation_id->get_value());
			$clusterisation_param->sorting->set_value($characteristic->sort);
			$clusterisation_param->characteristic_type_id->set_value($characteristic->id);
			$dba->save($clusterisation_param);
		}
		return $clusterisation->clusterisation_id->get_value();
	}
}

	?>